package com.health.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.health.tools.GlobalConts;
import com.tencent.bugly.Bugly;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.db.sqlite.DbModel;

public class FDbManager {
	// 数据库存放的文件夹
	private static String pathStr = Environment.getExternalStorageDirectory() + GlobalConts.TYPE_DBFILENAME;
	private static FinalDb db;
	private static FinalDb infoDb;
	private static FinalDb novaDb;

	private FDbManager() {
	}

	private static class LazyHolder {
		private static final FDbManager INSTANCE = new FDbManager();

	}

	public static void dropDB() {
		// 删除KioskDB.db-journal文件
		// String dbPath_journal =
		// Environment.getExternalStorageDirectory().getAbsolutePath()+GlobalConts.TYPE_DBFILENAME+
		// "/KioskDB.db-journal";
		// File f_journal = new File(dbPath_journal);
		// if (f_journal.exists()) {
		// f_journal.delete();
		// }
		// try {
		if (null != db) {
			// db.dropDb();
			db = null;
		}
		if (null != infoDb) {
			// infoDb.dropDb();
			infoDb = null;
		}
		// if (null != novaDb) {
		// // novaDb.dropDb();
		// infoDb = null;
		// }
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

	}

	public static final void initDB() {
		db = null;
		infoDb = null;
		infoDb = null;
	}

	public static final void init(Context context) {
		dropDB();
		db = db == null ? getDb(context, "KioskDB.db") : db;
		infoDb = infoDb == null ? getDb(context, "bd_dd.db") : infoDb;
		novaDb = novaDb == null ? FinalDb.create(context, "nova.db") : novaDb;
	}

	public static final FDbManager getInstance(Context context) {
		// dropDB();
		db = db == null ? getDb(context, "KioskDB.db") : db;
		infoDb = infoDb == null ? getDb(context, "bd_dd.db") : infoDb;
		novaDb = novaDb == null ? FinalDb.create(context, "nova.db") : novaDb;
		return LazyHolder.INSTANCE;
	}

	// "KioskDB.db"
	private static FinalDb getDb(Context context, String dbName) {
		// 删除KioskDB.db-journal文件
		String dbPath_journal = Environment.getExternalStorageDirectory().getAbsolutePath()
				+ GlobalConts.TYPE_DBFILENAME + "/KioskDB.db.back";
		File f_journal = new File(dbPath_journal);
		if (f_journal.exists()) {
			f_journal.renameTo(new File(Environment.getExternalStorageDirectory().getAbsolutePath()
					+ GlobalConts.TYPE_DBFILENAME + "/KioskDB.db"));
			// f_journal.delete();
			f_journal.setWritable(true);
		}
		// 数据库存储路径
		String filePath = pathStr + "/" + dbName;
		// 数据库存放的文件夹
		File jhPath = new File(filePath);// TODO 需要重新读取数据库
		if (!jhPath.exists()) {
			Log.i(dbName, "数据库不存在");
			// 存在则直接返回打开的数据库
			File path = new File(pathStr);
			// Log.i("KioskDB", "pathStr=" + path);
			if (path.mkdirs()) {
				Log.i(dbName, "创建成功");
			} else {
				Log.i(dbName, "创建失败");
			}

			try {
				// 得到资源
				AssetManager am = context.getAssets();
				// 得到数据库的输入流
				InputStream is = am.open(dbName);
				// 用输出流写到SDcard上面
				FileOutputStream fos = new FileOutputStream(jhPath);
				// 创建byte数组 用于1KB写一次
				byte[] buffer = new byte[1024];
				int count = 0;
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
				}
				// 最后关闭就可以了
				fos.flush();
				fos.close();
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();

			}
		}
		FinalDb db = FinalDb.create(context, pathStr, dbName);
		return db;
	}

	public void save(Object obj) {
		db.save(obj);
	}

	public <T> List<T> findAll(Class<T> clazz) {

		return db.findAll(clazz);
	}

	public <T> List<T> findAll(Class<T> clazz, String orderBy) {
		return db.findAll(clazz, orderBy);
	}

	public <T> List<T> findAllByWhere(Class<T> clazz, String strWhere) {
		try {
			return db.findAllByWhere(clazz, strWhere);
		} catch (android.database.sqlite.SQLiteDiskIOException e) {
			e.printStackTrace();
			return new ArrayList<T>();
		}
	}

	public <T> List<T> findAllByWhere(Class<T> clazz, String strWhere, String orderBy) {
		return db.findAllByWhere(clazz, strWhere, orderBy);
	}

	public <T> Object findById(Object id, Class<T> clazz) {
		return db.findById(id, clazz);
	}

	public DbModel findDbModelBySQL(String strSQL) {
		return db.findDbModelBySQL(strSQL);
	}

	public List<DbModel> findDbModelListBySQL(String strSQL) {
		return db.findDbModelListBySQL(strSQL);
	}

	public void delete(Object entity) {
		db.delete(entity);
	}

	public void updata(Object entity) {
		db.update(entity);
	}

	public void updata(Object entity, String strWhere) {
		db.update(entity, strWhere);
	}

	public void saveInInfoDB(Object obj) {
		infoDb.save(obj);
	}

	public <T> List<T> findAllInInfoDB(Class<T> clazz) {

		return infoDb.findAll(clazz);
	}

	public <T> List<T> findAllInInfoDB(Class<T> clazz, String orderBy) {
		return infoDb.findAll(clazz, orderBy);
	}

	public <T> List<T> findAllByWhereInInfoDB(Class<T> clazz, String strWhere) {
		return infoDb.findAllByWhere(clazz, strWhere);
	}

	public <T> List<T> findAllByWhereInInfoDB(Class<T> clazz, String strWhere, String orderBy) {
		return infoDb.findAllByWhere(clazz, strWhere, orderBy);
	}

	public <T> Object findByIdInInfoDB(Object id, Class<T> clazz) {
		return infoDb.findById(id, clazz);
	}

	public DbModel findDbModelBySQLInInfoDB(String strSQL) {
		return infoDb.findDbModelBySQL(strSQL);
	}

	public List<DbModel> findDbModelListBySQLInInfoDB(String strSQL) {
		return infoDb.findDbModelListBySQL(strSQL);
	}

	public void deleteInInfoDB(Object entity) {
		infoDb.delete(entity);
	}

	public void updataInInfoDB(Object entity) {
		infoDb.update(entity);
	}

	public void updataInInfoDB(Object entity, String strWhere) {
		infoDb.update(entity, strWhere);
	}
	
	//-------------------保存配对信息--------start----------------

	public void saveBT(Object obj) {
		novaDb.save(obj);
	}

	public <T> List<T> findAllBT(Class<T> clazz) {

		return novaDb.findAll(clazz);
	}

	public <T> List<T> findAllBT(Class<T> clazz, String orderBy) {
		return novaDb.findAll(clazz, orderBy);
	}

	public <T> List<T> findAllBTByWhere(Class<T> clazz, String strWhere) {
		return novaDb.findAllByWhere(clazz, strWhere);
	}

	public <T> List<T> findAllBTByWhere(Class<T> clazz, String strWhere, String orderBy) {
		return novaDb.findAllByWhere(clazz, strWhere, orderBy);
	}

	public <T> Object findBTById(Object id, Class<T> clazz) {
		return novaDb.findById(id, clazz);
	}

	public DbModel findBTDbModelBySQL(String strSQL) {
		return novaDb.findDbModelBySQL(strSQL);
	}

	public List<DbModel> findBTDbModelListBySQL(String strSQL) {
		return novaDb.findDbModelListBySQL(strSQL);
	}

	public void deleteBT(Object entity) {
		novaDb.delete(entity);
	}

	public void updataBT(Object entity) {
		novaDb.update(entity);
	}

	public void updataBT(Object entity, String strWhere) {
		novaDb.update(entity, strWhere);
	}

	//-------------------保存配对信息----end--------------------
	public <T> void deleteByWhere(Class<T> clazz, String strWhere) {
		db.deleteByWhere(clazz, strWhere);
	}

	public <T> void deleteAll(Class<T> clazz) {
		db.deleteAll(clazz);
	}
}